package com.lightboat.system.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lightboat.common.api.BaiDuJianKangApi;
import com.lightboat.common.config.properties.OpenApiCredentialProperties;
import com.lightboat.common.enums.OrderType;
import com.lightboat.common.utils.DateUtils;
import com.lightboat.system.domain.CrmLogisticsTrack;
import com.lightboat.system.domain.CrmOrder;
import com.lightboat.system.mapper.CrmLogisticsTrackMapper;
import com.lightboat.system.mapper.CrmOrderMapper;
import com.lightboat.system.service.ICrmLogisticsTrackService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 物流轨迹日志Service业务层处理
 *
 * @author lightboat
 * @date 2025-08-30
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CrmLogisticsTrackServiceImpl extends ServiceImpl<CrmLogisticsTrackMapper, CrmLogisticsTrack> implements ICrmLogisticsTrackService {

    private final CrmLogisticsTrackMapper crmLogisticsTrackMapper;

    private final CrmOrderMapper crmOrderMapper;
    private final OpenApiCredentialProperties apiCredentialProperties;

    /**
     * 查询物流轨迹日志
     *
     * @param trackId 物流轨迹日志主键
     * @return 物流轨迹日志
     */
    @Override
    public CrmLogisticsTrack selectCrmLogisticsTrackByTrackId(Long trackId) {
        return crmLogisticsTrackMapper.selectById(trackId);
    }

    /**
     * 查询物流轨迹日志列表
     *
     * @param crmLogisticsTrack 物流轨迹日志
     * @return 物流轨迹日志
     */
    @Override
    public List<CrmLogisticsTrack> selectCrmLogisticsTrackList(CrmLogisticsTrack crmLogisticsTrack) {
        List<CrmLogisticsTrack> list = crmLogisticsTrackMapper.selectCrmLogisticsTrackList(crmLogisticsTrack);
        if (ObjUtil.isEmpty(list)) {
            CrmOrder crmOrder = crmOrderMapper.selectOne(Wrappers.<CrmOrder>lambdaQuery().eq(CrmOrder::getLogisticsNo, crmLogisticsTrack.getWaybillNo()));
            if (ObjUtil.isNotEmpty(crmOrder) && crmOrder.getOrderType().equals(OrderType.SYSTEM.getType())) {
                if (crmOrder.getPlatform().equals("baidu")) {
                    BaiDuJianKangApi.OrderDetailRequest orderDetailRequest = new BaiDuJianKangApi.OrderDetailRequest(crmOrder.getStoreId(), crmOrder.getExternalOrderId());
                    BaiDuJianKangApi.DataRequest<BaiDuJianKangApi.OrderDetailRequest> dataRequest = new BaiDuJianKangApi.DataRequest<BaiDuJianKangApi.OrderDetailRequest>(orderDetailRequest);
                    String dataJson = JSON.toJSONString(dataRequest);
                    JSONObject sendResult = BaiDuJianKangApi.send(apiCredentialProperties.getBaidus().get(0), null, BaiDuJianKangApi.ORDER_DETAIL, dataJson);
                    log.info("请求结果:{}", sendResult);
                    if (sendResult != null && sendResult.getIntValue("status", -1) == 0) {
                        BaiDuJianKangApi.OrderDetailResponse orderDetailResponse = sendResult.getObject("data", BaiDuJianKangApi.OrderDetailResponse.class);
                        JSONArray deliverInfos = orderDetailResponse.getDeliverInfos();
                        if (ObjUtil.isNotEmpty(deliverInfos)) {
                            list = deliverInfos.stream().map(m -> {
                                JSONObject jsonObject = (JSONObject) m;
                                String description = jsonObject.getString("description");
                                CrmLogisticsTrack crmLogisticsTrack1 = new CrmLogisticsTrack();
                                crmLogisticsTrack1.setOperationRemark(description);
                                crmLogisticsTrack1.setOperationTime(jsonObject.getDate("time"));
                                return crmLogisticsTrack1;
                            }).toList();
                        }

                    }
                }
            }

        }
        return list;
    }

    /**
     * 新增物流轨迹日志
     *
     * @param crmLogisticsTrack 物流轨迹日志
     * @return 结果
     */
    @Override
    public int insertCrmLogisticsTrack(CrmLogisticsTrack crmLogisticsTrack) {
        crmLogisticsTrack.setCreateTime(DateUtils.getNowDate());
        return crmLogisticsTrackMapper.insert(crmLogisticsTrack);
    }

    /**
     * 修改物流轨迹日志
     *
     * @param crmLogisticsTrack 物流轨迹日志
     * @return 结果
     */
    @Override
    public int updateCrmLogisticsTrack(CrmLogisticsTrack crmLogisticsTrack) {
        crmLogisticsTrack.setUpdateTime(DateUtils.getNowDate());
        return crmLogisticsTrackMapper.updateById(crmLogisticsTrack);
    }

    /**
     * 批量删除物流轨迹日志
     *
     * @param trackIds 需要删除的物流轨迹日志主键
     * @return 结果
     */
    @Override
    public int deleteCrmLogisticsTrackByTrackIds(Long[] trackIds) {
        return crmLogisticsTrackMapper.deleteCrmLogisticsTrackByTrackIds(trackIds);
    }

    /**
     * 删除物流轨迹日志信息
     *
     * @param trackId 物流轨迹日志主键
     * @return 结果
     */
    @Override
    public int deleteCrmLogisticsTrackByTrackId(Long trackId) {
        return crmLogisticsTrackMapper.deleteCrmLogisticsTrackByTrackId(trackId);
    }
}
